import java.awt.*;
import java.io.*;
/**
 * Representing one player.
 *
 * @author dbos
 * @version 2008-04-18
 */
public class Player implements Serializable
{
    // the piece currently controlled
    private Piece currentPiece;
    // player id to avoid name collision
    private int id;
    // the name of the player
    //private String name;
    // a PieceGenerator
    private PieceGenerator pieceGenerator;
    // integer vector containing powerups for the player, powerups[x]=>0 means emty slot
    private int[] powerups;
    private int peeking;

	private boolean disabled = false;


	/**
	 * The number of turns left that a single brick will be generated instead of a "real" piece.
	 */
    private int singleBrickTurns = 0;


    /**
     * Constructor for objects of class Player
     */
    public Player(int id, int slots, int seed)
    {
        //this.name = name;
        this.id = id;
        this.powerups = new int[slots];
        this.pieceGenerator = new PieceGenerator(this, seed*(id+1));
    }

	/*public String getName() {
		return name;
	}*/

    /**
     * Retreive the piece that is controlled by the player
     *
     * @return     a Piece
     */
    public Piece getCurrentPiece()
    {
        return currentPiece;
    }

    /**
     * Retreive the powerups of a player
     *
     * @return     a vector possibly containing powerups
     */
    public int[] getPowerUps()
    {
        return powerups;
    }

    /**
     * Remove a powerup when used
     *
     * @param  index  index to remove (zero-indexed)
     * @return  removed powerup number, 0 if no powerup existed in index.
     */
    public int removePowerup(int index)
    {
        if(0 <= index && index < powerups.length ) {
            int toRemove = powerups[index];
            powerups[index] = 0;
            return toRemove;
        }
        else return -1;
    }

    /**
     * Get a powerup
     *
     * @param  index  index to remove (zero-indexed)
     * @return  powerup number, 0 if no powerup existed in index.
     */
    public int getPowerup(int index)
    {
        if(0 <= index && index < powerups.length ) {
            return powerups[index];
        }
        else return -1;
    }

    /**
     * Add a powerup to inventory
     *
     * @param  powerup  number representing a powerup
     * @return false if there are no free slots, true if added successfully
     */
    public boolean addPowerup(int powerup)
    {
    	/* first, look for an empty slot */
        for(int i = 0; i < powerups.length; i++) {
            if(powerups[i] == 0){
                powerups[i] = powerup;
                return true;
            }
        }
        /* No empty slots? Are there any useless C-powerups we can replace? */
        for(int i = 0; i < powerups.length; i++) {
        	if(
        		(powerup == Multitris.POWERUP_N && (powerups[i]==Multitris.POWERUP_C || powerups[i]==Multitris.POWERUP_S)) ||
        		(powerup == Multitris.POWERUP_G && (powerups[i]==Multitris.POWERUP_C || powerups[i]==Multitris.POWERUP_S)) ||
        		(powerup == Multitris.POWERUP_S && powerups[i]==Multitris.POWERUP_C) ) {
        		powerups[i] = powerup;
        		return true;
            }
        }
        return false;
    }

    /**
     * Generate a new piece for the player
     */
    public void newPiece(Point point)
    {
        currentPiece = pieceGenerator.getNext();
        currentPiece.setPoint(point);

    }

    /**
     * Returns the next piece the player will get
	 *
	 * @param  offset  the offset for the piece to be peeked at
	 * @return  piece
     */
    public Piece peekNext(int offset)
    {
		return pieceGenerator.peekNext(offset);
    }

    public int getId()
    {
		return id;
    }

	/**
	 * Changes a players id. Use with caution!
	 */
	public void setId(int id)
	{
		this.id = id;
	}

    public void setPeeking(int value)
    {
    	peeking = value;
    }

    public int getPeeking()
    {
    	return peeking;
    }

	public boolean isPeeking() {
		return peeking > 0;
	}

    /**
     * Enables the split piece powerup.
     */
    public void useSplit() {
		if(singleBrickTurns > 0) {
			singleBrickTurns--;
		}
	}

    /**
     * Enables the split piece powerup.
     */
    public boolean isSplitting() {
		if(singleBrickTurns > 0) {
			return true;
		}
		return false;
	}

    /**
     * Enables the split piece powerup.
     */
    public void enableSplit() {
		singleBrickTurns += 4;
	}

    /**
     * Disables the player so it won't recieve any new pieces.
     */
    public void disable() {
		disabled = true;
	}
	
    public void undisable() {
		disabled = false;
	}

    public boolean isDisabled() {
		return disabled;
	}
}